package com.ruoyi.web.controller.Order;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Order.domain.Order;
import com.ruoyi.Order.service.IOrderService;
import com.ruoyi.School.domain.School;
import com.ruoyi.School.service.ISchoolService;
import com.ruoyi.User.domain.User;
import com.ruoyi.User.service.IUserService;
import com.ruoyi.UserAddress.domain.UserAddress;
import com.ruoyi.UserAddress.service.IUserAddressService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.APPConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysDept;

/**
 * 订单 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-07-15
 */
@Controller
@RequestMapping("/Order/finishedOrder")
public class FinishedOrderController extends BaseController
{
    private String prefix = "Order/finishedOrder";
	
    @Autowired
	private IOrderService orderService;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private IUserAddressService userAddressService;
	
	@Autowired
	private ISchoolService schoolService;
	
	@RequiresPermissions("Order:order:view")
	@GetMapping()
	public String order()
	{
	    return prefix + "/order";
	}
	
	/**
	 * 查询订单列表
	 */
	@RequiresPermissions("Order:order:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Order order)
	{
		String deptId=getDeptId();
		if(!"100".equals(deptId)){
			order.setDeptId(Integer.parseInt(deptId));
		}
		order.setType(APPConstants.ORDER_TYPE_SHOPPING);
		order.setOrderShuxin("2005");
		order.setValid(3);
		startPage();
        List<Order> list = orderService.selectOrderList(order);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出订单列表
	 */
	@RequiresPermissions("Order:order:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Order order)
    {
    	List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "order");
    }
	
	/**
	 * 新增订单
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存订单
	 */
	@RequiresPermissions("Order:order:add")
	@Log(title = "订单", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Order order)
	{		
		return toAjax(orderService.insertOrder(order));
	}

	/**
	 * 修改订单
	 */
	/**
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
		Order order = orderService.selectOrderById(id);
		String jiadanId = order.getJiadanId();
		if (!StringUtils.isEmpty(jiadanId)) {
			int jiadanidToInt = Integer.parseInt(jiadanId);
			Order example = new Order();
			example.setId(jiadanidToInt);
			List<Order> orderList = orderService.selectOrderList(example);
			if (orderList.size() > 0) {
				Order jiadanOrder = orderList.get(0);
				mmap.put("jiadanOrder", jiadanOrder);
			} else {
				mmap.put("jiadanOrder", new Order());
			}
		} else {
			mmap.put("jiadanOrder", new Order());
		}
		String addressId = order.getAddressId();
		UserAddress userAddress = new UserAddress();
		if(!StringUtils.isEmpty(addressId)){
			int addressIdToInt = Integer.parseInt(addressId);
			userAddress = userAddressService
					.selectUserAddressByIdChinese(addressIdToInt);			
		} 		
		mmap.put("userAddress", userAddress==null?new UserAddress():userAddress);
		Integer qishouId = order.getQishouId();
		User riderUser =new User();
		if(qishouId!= null){
			riderUser = userService.selectUserById(qishouId);		
		}	
		mmap.put("riderUser", riderUser==null?new User():riderUser);
		mmap.put("order", order);
		return prefix + "/edit";
	}
	
	
	
	
	@GetMapping("/orderGoods/{id}")
	public String orderGoods(@PathVariable("id") Integer id, ModelMap mmap)
	{
		mmap.put("orderId", id);
	    return prefix + "/orderGoods";
	}
	
	
	
	/**
	 * 修改保存订单
	 */
	@RequiresPermissions("Order:order:edit")
	@Log(title = "订单", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Order order)
	{		
		return toAjax(orderService.updateOrder(order));
	}
	
	/**
	 * 删除订单
	 */
	@RequiresPermissions("Order:order:remove")
	@Log(title = "订单", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(orderService.deleteOrderByIds(ids));
	}
	
	//schoolId改拿部门Id
		public String getDeptId() {
			SysDept dept=ShiroUtils.getSysUser().getDept();
			if(dept!=null){
				String id=dept.getDeptId().toString();
				return id;						
			} else {
				return StringUtils.EMPTY;
			}
		}
	
	
	
	
}
